﻿using HCMS.Entities.Manage;
using HCMS.IRepositories.Manage;
using HCMS.IServices.Manage;
using HCMS.Repositories.Manage;
using System;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;

namespace HCMS.Services.Manage
{
    /// <summary>
    /// 管理员 角色 应用服务
    /// </summary>
    public class AdminRoleAppService : CommonAppService<ManageDbContext, AdminRole>, IAdminRoleAppService
    {
        private readonly IRepository<AdminRole> baseAdminRoleRepository;
        private readonly IAdminRoleRepository adminRoleRepository;
        private readonly IRoleRepository roleRepository;
        private readonly IAdminRepository adminRepository;

        public AdminRoleAppService(
            IRepository<AdminRole> baseAdminRoleRepository,
            IAdminRoleRepository adminRoleRepository,
            IRoleRepository roleRepository,
            IAdminRepository adminRepository
            ) : base(baseAdminRoleRepository)
        {
            this.baseAdminRoleRepository = baseAdminRoleRepository;
            this.adminRoleRepository = adminRoleRepository;
            this.roleRepository = roleRepository;
            this.adminRepository = adminRepository;
        }

        /// <summary>
        /// 设置管理员 角色
        /// </summary>
        /// <param name="adminId">管理员 Id</param>
        /// <param name="roleId">角色 Id</param>
        /// <param name="createUser">创建 人</param>
        /// <returns></returns>
        public async Task<bool> SetAdminRole(int adminId, int roleId, string createUser)
        {
            AdminRole? result = null;

            var admin = await adminRepository.FirstOrDefaultAsync(t => t.Id == adminId);
            if (admin == null || admin.Status == -1)
            {
                throw new UserFriendlyException("没有找到管理员");
            }

            var role = await roleRepository.FirstOrDefaultAsync(t => t.Id == roleId);
            if (role == null || role.Status != 0)
            {
                throw new UserFriendlyException("没有找到角色");
            }

            var adminRole = await BaseFirstOrDefaultAsync(t => t.AdminId == adminId);
            if (adminRole == null)
            {
                adminRole = new AdminRole()
                {
                    CreateTime = DateTime.Now,
                    CreateUser = createUser,
                    UpdateTime = DateTime.Now,
                    UpdateUser = createUser,
                    Status = 0,
                    AdminId = adminId,
                    RoleId = roleId
                };

                result = await BaseInsertAsync(adminRole);
            }
            else
            {
                adminRole.RoleId = roleId;
                adminRole.UpdateTime = DateTime.Now;
                adminRole.UpdateUser = createUser;
                adminRole.Status = 0;

                result = await BaseUpdateAsync(adminRole);
            }

            return result != null;
        }

        /// <summary>
        /// 获取 管理员 角色 Id
        /// </summary>
        /// <param name="adminId">管理员 Id</param>
        /// <returns>0：未配置角色。其它Id：有配置角色</returns>
        public async Task<int> GetAdminRoleId(int adminId)
        {
            var adminRole = await BaseFirstOrDefaultAsync(t => t.AdminId == adminId);
            if (adminRole == null || adminRole.Status != 0)
            {
                return 0;
            }

            var role = await roleRepository.FirstOrDefaultAsync(t => t.Id == adminRole.RoleId);
            if (role == null || role.Status != 0)
            {
                return 0;
            }

            return role.Id;
        }
    }
}
